home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
AOL File Library: 2,801 to 2,900
/
aol-file-protocol-4400-2801-to-2900.zip
/
AOLDLs
/
C++ Files Library
/
Linked List Template Classes
/
Linked Lists Ä.sit
/
Linked Lists ƒ
/
source code
/
LinkedLists.cp
next >
Wrap
Text File
|
1995-03-03
|
7KB
|
283 lines
#include "LinkedLists.h"
/***************************************************
SlimListClass: Simple Pop and Push Features
***************************************************/
template <class itemRecord> Boolean SlimListClass<itemRecord>::Push(itemRecord &theItem)
{
ListItem<itemRecord> *newItem;
newItem=new ListItem<itemRecord>;
newItem->item=theItem;
if (newItem) {
newItem->next=theList;
theList=newItem; // theList variable always points to the top of the stack
return 1;
}
return 0;
}
template <class itemRecord> itemRecord SlimListClass<itemRecord>::Pop()
{
if (theList) {
itemRecord thisItem=theList->item;
ListItem<itemRecord> *temp=theList->next;
delete theList;
theList=temp;
return thisItem;
}
return theList->item;
}
template <class itemRecord> void SlimListClass<itemRecord>::DeleteAll()
{
ListItem<itemRecord> *temp=nil;
if (theList) {
temp=theList->next;
delete theList;
theList=temp;
}
while (temp) {
temp=theList->next;
delete theList;
theList=temp;
}
}
/***************************************************
SlimPlusClass: Adds Item Retrieval
***************************************************/
template <class itemRecord, class comparisonObject> Boolean SlimPlusClass<itemRecord, comparisonObject>::ItemInList(itemRecord &theItem)
{
ListItem<itemRecord> *top;
top=theList;
if (theList->item==theItem)
return 1;
while ((theList=theList->next)!=nil)
if (theList->item==theItem) {
theList=top; // Restore theList var to the stack top address
return 1;
}
theList=top; // Restore theList var to the stack top address
return 0;
}
template <class itemRecord, class comparisonObject> Boolean SlimPlusClass<itemRecord, comparisonObject>::XInList(comparisonObject theObject, itemRecord * &theItem)
{
ListItem<itemRecord> *top;
top=theList;
if (theList) {
if (theList->item==theObject) {
theItem=&(theList->item); // Set theItem to the address of the item's address
return 1;
}
else
while ((theList=theList->next)!=nil) {
if (theList->item==theObject) {
theItem=&(theList->item); // Set theItem to the address of the item's address
theList=top; // Restore theList var to the stack top address
return 1;
}
}
}
theList=top; // Restore theList var to the stack top address
return 0;
}
/***************************************************
ListClass : Full-Featured List Class
***************************************************/
template <class itemRecord> Boolean ListClass<itemRecord>::AddSort(itemRecord &theItem)
{
ListItem<itemRecord> *newItem, *tempItem;
placeHolder=newItem=new ListItem<itemRecord>;
if (newItem) {
numberOfItems++;
newItem->item=theItem;
ListItem<itemRecord> *tempItem;
if (stackTop) { // If stackTop is not nil, a stack is present
if (theItem<stackTop->item) { // Set newItem->next to stackTop if it's less and
newItem->next=stackTop; // set stackTop to newItem
stackTop=newItem;
}
else {
tempItem=stackTop; // Otherwise, put insert it in first spot it's less
// than an other item
while (theItem>tempItem->next->item && tempItem->next)
tempItem=tempItem->next;
newItem->next=tempItem->next;
tempItem->next=newItem;
}
}
else { // Stack is created, set stackTop to newItem
stackTop=newItem;
newItem->next=nil;
}
return (1);
}
return (0);
}
template <class itemRecord> Boolean ListClass<itemRecord>::Add(itemRecord &theItem)
{
ListItem<itemRecord> *newItem, *tempItem;
placeHolder=newItem=new ListItem<itemRecord>;
if (newItem) {
numberOfItems++;
newItem->item=theItem;
if (stackTop) { // Go through whole list until end is found, add
tempItem=stackTop; // newItem to the end
while (tempItem->next)
tempItem=tempItem->next;
tempItem->next=newItem;
}
else
stackTop=newItem;
newItem->next=nil; // Set newItem->next to nil, connotating end of list
return 1;
}
return 0;
}
template <class itemRecord> Boolean ListClass<itemRecord>::Push(itemRecord &theItem)
{
ListItem<itemRecord> *newItem;
placeHolder=newItem=new ListItem<itemRecord>;
if (newItem) { // Essentially, this is the same as SlimListClasses,
numberOfItems++; // except that it updates the placeHolder var and the
newItem->item=theItem; // numberOfItems var
newItem->next=stackTop;
stackTop=newItem;
return 1;
}
return 0;
}
template <class itemRecord> itemRecord ListClass<itemRecord>::Pop()
{
itemRecord theItem;
ListItem<itemRecord> *temp=stackTop;
theItem=stackTop->item;
placeHolder=stackTop=stackTop->next;
delete temp;
numberOfItems--;
return theItem;
}
template <class itemRecord> void ListClass<itemRecord>::Delete(itemRecord theItem)
{
ListItem<itemRecord> *tempItem=stackTop;
if (tempItem->item==theItem) {
placeHolder=stackTop=stackTop->next;
delete tempItem;
numberOfItems--;
}
else {
while (tempItem->next->item!=theItem && tempItem)
tempItem=tempItem->next;
if (tempItem) {
ListItem<itemRecord> *oldItem=tempItem->next;
placeHolder=tempItem->next=tempItem->next->next;
delete oldItem;
numberOfItems--;
}
}
}
template <class itemRecord> void ListClass<itemRecord>::DeleteAll()
{
ListItem<itemRecord> *temp=stackTop;
while (temp) { // Essentially the same as the SlimListClass DeleteAll
temp=stackTop->next;
delete stackTop;
stackTop=temp;
}
numberOfItems=0;
placeHolder=stackTop;
}
template <class itemRecord> ListItem<itemRecord> *ListClass<itemRecord>::GetPlaceItem(itemRecord * &theItem)
{
if (placeHolder)
theItem=&(placeHolder->item);
return placeHolder;
}
template <class itemRecord> ListItem<itemRecord> *ListClass<itemRecord>::GetNextItem(itemRecord * &theItem)
{
if (placeHolder) // Check to make sure placeHolder is valid address
placeHolder=placeHolder->next;
if (placeHolder) // Check to make sure it still is, after pointing to the next
theItem=&(placeHolder->item); // If everything checks out, set second parameter to address
return placeHolder; // of placeHolder's item
}
template <class itemRecord> Boolean ListClass<itemRecord>::GetItemX(short itemNumber, itemRecord * &theItem)
{
ListItem<itemRecord> *temp=stackTop;
short n=1;
if (itemNumber>numberOfItems)
return 0;
while (n++!=itemNumber)
temp=temp->next;
theItem=&(temp->item);
return 1;
}